package com.example.demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by czc-pual on 2018/12/14.
 */
public class test {

    public static void main(String[] args) throws ParseException {
        String year = "2000";
        System.out.println(year+"年------"+"2018年中"+"\n");
        for(int i=Integer.parseInt(year);i<=2018;i++){
            String result = mymain(String.valueOf(i));

            if (result!=null){
                if((i%4==0&&i%100!=0)||i%400==0)
                    System.out.println(i+"是闰年。");
                else
                    System.out.println(i+"是平年。");
//                System.out.println("result:"+result);
            }

        }

    }

    public static String mymain(String year) throws ParseException {
        String var1=null,var2=null,var3=null,var4=null,var5=null,var6=null,var7=null,var8=null,var9=null,var10=null,var11=null,var12=null;
        for(int a=0;a<12;a++){
            var1 = year+"-01-01";
            var2 = year+"-02-01";
            var3 = year+"-03-01";
            var4 = year+"-04-01";
            var5 = year+"-05-01";
            var6 = year+"-06-01";
            var7 = year+"-07-01";
            var8 = year+"-08-01";
            var9 = year+"-09-01";
            var10 = year+"-10-01";
            var11 = year+"-11-01";
            var12 = year+"-12-01";
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse(var1);
        Date date2 = sdf.parse(var2);
        Date date3 = sdf.parse(var3);
        Date date4 = sdf.parse(var4);
        Date date5 = sdf.parse(var5);
        Date date6 = sdf.parse(var6);
        Date date7 = sdf.parse(var7);
        Date date8 = sdf.parse(var8);
        Date date9 = sdf.parse(var9);
        Date date10 = sdf.parse(var10);
        Date date11 = sdf.parse(var11);
        Date date12 = sdf.parse(var12);

        List<Date> dates = null;dates = new ArrayList<Date>();
        dates.add(date1);
        dates.add(date2);
        dates.add(date3);
        dates.add(date4);
        dates.add(date5);
        dates.add(date6);
        dates.add(date7);
        dates.add(date8);
        dates.add(date9);
        dates.add(date10);
        dates.add(date11);
        dates.add(date12);
        //123 234 345 456
        //总天数
        for(int v=0;v<10;v++){
            int all = 0;
            String flag = my(v,all,dates);
            if (flag!=null){
                System.out.println("从"+year+"年"+(v+1)+"月开始至"+year+"年"+(v+3)+"月结束："+"开始时间："+flag);
                return flag;
            }
        }
        return null;
    }


    public static String my(int v,int all,List<Date> dates){
        Date currentDate = null;
        List list = new ArrayList();
        for(int i=0;i<3;i++){
            currentDate = dates.get(v);
            //这个月份有多少个星期六和星期日
            list = dayReport(currentDate);
            all = all + (int)list.get(list.size()-1);
            v++;
        }
        if (all == 24){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            //最后的休息是几月几号
            List<String> strings = (List<String>) list.get(0);
            Collections.reverse(strings); // 倒序排列
            System.out.println(strings.get(0));
            return sdf.format(currentDate);
        }
        return null;
    }

    public static int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    public static List dayReport(Date month) {
        List list = new ArrayList();
        List<String> strings = new ArrayList<>();
        int num = 0;
        Calendar cal = Calendar.getInstance();
        cal.setTime(month);//month 为指定月份任意日期
        int year = cal.get(Calendar.YEAR);
        int m = cal.get(Calendar.MONTH);
        int dayNumOfMonth = getDaysByYearMonth(year, m);
        cal.set(Calendar.DAY_OF_MONTH, 1);// 从一号开始
        for (int i=0;i<dayNumOfMonth;i++, cal.add(Calendar.DATE, 1)){
            Date d = cal.getTime();
            //计算星期六和星期天的个数
            String weekDate = getWeekOfDate(d);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (weekDate.equals("星期六") || weekDate.equals("星期日")){
                strings.add("最后一个休息日是："+sdf.format(d)+" :"+weekDate);
                num ++;
            }
            list.add(strings);
            list.add(num);
        }
        return list;
    }

    public static String getWeekOfDate(Date date) {
        String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar = Calendar.getInstance();
        if(date != null){
            calendar.setTime(date);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0){
            w = 0;
        }
        return weekOfDays[w];

    }


}
